﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Pulog.Models
{
    public partial class User
    {
        private PulogDataContext dc = new PulogDataContext();
        public User GetUserByID(int? id)
        {
            return dc.Users.Single<User>(u => u.userID == id);
        }

        public bool Login(string username, string password)
        {
            if ((dc.Users.Where<User>(u => u.userName == username && u.password == Helper.GetMD5Hash(password) && u.isAdmin==true).ToList<User>()).Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        

        public User CreateUser(string username, string password, string email)
        {
            try
            {
                User user = new User()
                {
                    userName = username,
                    password = password,
                    email = email
                };
                dc.Users.InsertOnSubmit(user);
                dc.SubmitChanges();
                return user;
            }
            catch
            {
                return null;
            }
        }

        public int GetUserIDByUserName(string userName)
        {
            return dc.Users.Single(u => u.userName == userName).userID;
        }
    }
}